package com.qidian.mes.produce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.Composition;
import com.qidian.mes.pojo.Material;
import com.qidian.mes.pojo.MaterialCraft;
import com.qidian.mes.produce.mapper.MaterialCraftMapper;
import com.qidian.mes.produce.mapper.MaterialMapper;
import com.qidian.mes.produce.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈俊成
 * @Company ccc
 * @Description
 * @Time 2024/10/23 9:18
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    MaterialCraftMapper materialCraftMapper;
    @Override
    public MESResult queryAllByRelId(Material material,int craftId, int page, int limit) {
        System.out.println("++++++++++++++++++++++++++++++++"+material);
        List<Integer> materialCrafts = materialCraftMapper.getList(craftId);

        PageHelper.startPage(page,limit);

        List<Material> materials =new ArrayList<>();
        for (int i=0;i<materialCrafts.size();i++){
            materials.add(materialMapper.getList(material,materialCrafts.get(i)));
        }

        PageInfo<Material> materialPageInfo = new PageInfo<>(materials);
        return MESResult.success(materialPageInfo);
    }

    @Override
    public MESResult addMaterial(int materialRelId,int craftRelId) {
        if (materialCraftMapper.getId(craftRelId,materialRelId)!=0){
            return MESResult.error();
        }
        return MESResult.success(materialCraftMapper.addMaterialCraft(craftRelId,materialRelId));
    }

    @Override
    public MESResult primaryMaterial(Material material) {
         if (materialCraftMapper.primary(material.getFlagId(),material.getMaterialRelId())==1){
             return MESResult.success();
         }
         return MESResult.error();
    }

    @Override
    public MESResult getPrimary(int materialId,int craftId) {
        int id = materialCraftMapper.getId(craftId,materialId);
        Material material = materialMapper.getPrimary(materialId);
        material.setFlagId(id);

        return MESResult.success(material);
    }

    @Override
    public MESResult getAll() {
        return MESResult.success(materialMapper.getAll());
    }
}
